﻿using Management.Auxiliary;
using Management.Core.EquipmentFile;
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Linq;

namespace Management.Areas.Control.Controllers
{
    [AdminAuthorize]
    public class EquipmentFileController : Controller
    {
        private EquipmentFileManager efm = new EquipmentFileManager();
        //
        // GET: /Control/EquipmentFile/
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="siliconCarbideId"></param>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public ActionResult PageList(int? equipmentFileId,string name, int? pageNumber, int? pageSize, int? order)
        {
            Paging<EquipmentFile> _pagingEquipmentFile = new Paging<EquipmentFile>();
            if (pageNumber != null && pageNumber > 0) _pagingEquipmentFile.PageIndex = (int)pageNumber + 1;
            if (pageSize != null && pageSize > 0) _pagingEquipmentFile.PageSize = (int)pageSize;
            var _paging = efm.FindPageList(_pagingEquipmentFile, equipmentFileId, name, null);
            return Json(new { total = _paging.TotalNumber, rows = _paging.Items });
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {
            List<SelectListItem> _listDisabled = new List<SelectListItem>(2);
            _listDisabled.Add(new SelectListItem() { Text = "开启", Value = "1" });
            _listDisabled.Add(new SelectListItem() { Text = "禁止", Value = "0" });
            List<SelectListItem> _listShutdown = new List<SelectListItem>(2);
            _listShutdown.Add(new SelectListItem() { Text = "长期停机", Value = "1" });
            _listShutdown.Add(new SelectListItem() { Text = "不长期停机", Value = "0" });
            var _equipmentAreas = new Management.Core.EquipmentArea.EquipmentAreaManager().FindList();
            List<SelectListItem> _listAreas = new List<SelectListItem>(_equipmentAreas.Count());
            foreach (var area in _equipmentAreas)
            {
                _listAreas.Add(new SelectListItem() { Text = area.Name, Value = area.EquipmentAreaID.ToString() });
            }
            var _equipmentAdmins = new Management.Core.AdministrationArea.AdministrationAreaManager().FindList();
            List<SelectListItem> _listAdmins = new List<SelectListItem>(_equipmentAdmins.Count());
            foreach (var admin in _equipmentAdmins)
            {
                _listAdmins.Add(new SelectListItem() { Text = admin.Name, Value = admin.AdministrationAreaID.ToString() });
            }
            ViewBag.IsDisabled = _listDisabled;
            ViewBag.IsAlwaysShutdown = _listShutdown;
            ViewBag.AdministrationAreas = _listAdmins;
            ViewBag.Areas = _listAreas;
            return PartialView();
        }

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="_equipmentFile"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        
        public ActionResult Add([Bind(Include = "EquipmentFileID, Name, Specification, genre, EquipmentAreaID, AdministrationAreaID, DepartmentID, WorkshopID, ProcessID, Manufacturer, IsAlwaysShutdown, ShutdownReason, DailyOutput, SpecificationID, IsDisabled, Remark, Created_at, Updated_at")]EquipmentFile _equipmentFile)
        {
            _equipmentFile.Created_at = DateTime.Now;
            _equipmentFile.Updated_at = DateTime.Now;
            Response _resp = new Response() { Code = -1 };
            if (ModelState.IsValid)
            {
                //检查名称
                if (efm.IsContains(u => u.Name == _equipmentFile.Name))
                {
                    _resp.Code = 0;
                    _resp.Message = "类型已存在";
                }
                else
                {
                    _resp = efm.Add(_equipmentFile);
                }
            }
            else
            {
                _resp.Code = 0;
                _resp.Message = General.GetModelErrorString(ModelState);
            }
            return Json(_resp);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="equipmentFileID"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(int equipmentFileID)
        {
            Response _resp = new Response();
            var _equipmentFile = efm.Find(equipmentFileID);
            if (_equipmentFile == null)
            {
                _resp.Code = 0;
                _resp.Message = "要删除条目不存在";
            }
            else
            {
                _resp = efm.Delete(_equipmentFile);
            }
            return Json(_resp);
        }

        public ActionResult Modify(int id)
        {
            List<SelectListItem> _listDisabled = new List<SelectListItem>(2);
            _listDisabled.Add(new SelectListItem() { Text = "开启", Value = "1" });
            _listDisabled.Add(new SelectListItem() { Text = "禁止", Value = "0" });
            List<SelectListItem> _listShutdown = new List<SelectListItem>(2);
            _listShutdown.Add(new SelectListItem() { Text = "长期停机", Value = "1" });
            _listShutdown.Add(new SelectListItem() { Text = "不长期停机", Value = "0" });
            var _equipmentAreas = new Management.Core.EquipmentArea.EquipmentAreaManager().FindList();
            List<SelectListItem> _listAreas = new List<SelectListItem>(_equipmentAreas.Count());
            foreach (var area in _equipmentAreas)
            {
                _listAreas.Add(new SelectListItem() { Text = area.Name, Value = area.EquipmentAreaID.ToString() });
            }
            var _equipmentAdmins = new Management.Core.AdministrationArea.AdministrationAreaManager().FindList();
            List<SelectListItem> _listAdmins = new List<SelectListItem>(_equipmentAdmins.Count());
            foreach (var admin in _equipmentAdmins)
            {
                _listAdmins.Add(new SelectListItem() { Text = admin.Name, Value = admin.AdministrationAreaID.ToString() });
            }
            ViewBag.IsDisabled = _listDisabled;
            ViewBag.IsAlwaysShutdown = _listShutdown;
            ViewBag.AdministrationAreas = _listAdmins;
            ViewBag.Areas = _listAreas;
            return PartialView(efm.Find(id));
        }

        [HttpPost]
        public ActionResult Modify(int id, FormCollection form)
        {
            Response _resp = new Response();
            var _equipmentFile = efm.Find(id);
            _equipmentFile.Updated_at = DateTime.Now;
            if (TryUpdateModel(_equipmentFile, new string[] { "EquipmentFileID"," Name"," Specification"," genre"," EquipmentAreaID"," AdministrationAreaID"," DepartmentID"," WorkshopID"," ProcessID"," Manufacturer"," IsAlwaysShutdown"," ShutdownReason"," DailyOutput"," SpecificationID"," IsDisabled"," Remark"," Created_at"," Updated_at" }))
            {
                if (efm.IsContains(u => u.Name == _equipmentFile.Name) && form["Name"] != _equipmentFile.Name)
                {
                    _resp.Code = 0;
                    _resp.Message = "名称已存在";
                }
                else
                {
                    _resp = efm.Update(_equipmentFile);
                }
            }
            else
            {
                _resp.Code = 0;
                _resp.Message = "模型不符合条件，更新失败";
            }
            return Json(_resp);
        }
	}
}